<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="http://117.80.114.220:81/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="http://117.80.114.220:81/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset" sec:authorize="hasAuthority('sys:role:query')">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" lay-filter="data-search-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">角色名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色编码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-clear-btn"><i class="layui-icon"></i> 重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarRole">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" sec:authorize="hasAuthority('sys:role:add')"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete" sec:authorize="hasAuthority('sys:role:delete')"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="roleTableId" lay-filter="roleTableFilter"></table>

        <script type="text/html" id="roleTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="role" sec:authorize="hasAuthority('sys:role:menu')">分配</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit" sec:authorize="hasAuthority('sys:role:update')">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" sec:authorize="hasAuthority('sys:role:delete')">删除</a>
        </script>

    </div>
</div>
<script src="http://117.80.114.220:81/static/lib/layui/layui.js" charset="utf-8"></script>
<script src="http://117.80.114.220:81/static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="http://117.80.114.220:81/static/js/my.js"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            baseURL = layui.baseUrl,
            table = layui.table;

        table.render({
            elem: '#roleTableId',
            url: baseURL + '/sys-role/list',
            toolbar: '#toolbarRole',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID'},
                {field: 'name', minWidth: 100, title: '菜单名称'},
                {field: 'code', minWidth: 100, title: '菜单编码'},
                {field: 'createdByName', minWidth: 100, title: '创建者'},
                {field: 'createdTime', minWidth: 200, title: '创建时间' },
                {field: 'updatedByName', minWidth: 100, title: '修改者'},
                {field: 'updatedTime', minWidth: 200, title: '修改时间'},
                {title: '操作', minWidth: 100, toolbar: '#roleTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('roleTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });
        // 监听重置操作
        form.on('submit(data-clear-btn)', function (data) {
            form.val('data-search-form',{name:"",code:""});
            //执行搜索重载
            table.reload('roleTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: {}
                }
            }, 'data');
            return false;
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(roleTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var searchParam = JSON.stringify(form.val('data-search-form'));
                var datas = table.checkStatus('roleTableId'); //id 即为基础参数 id 对应的值
                if(datas.data.length > 0){
                    //询问框
                    layer.confirm('确认要删除选中数据？', {
                        btn: ['确认','取消'] //按钮
                    }, function(){
                        var ids = [];
                        $.each(datas.data,function(i,e){
                            ids.push(e.id);
                        });
                        var userIds = JSON.stringify({roleIds:ids});
                        myPost("/sys-role/deleteByIds",userIds,function(data){
                            if(data.code == 0){
                                layer.msg('删除成功', {icon: 1});
                                //执行搜索重载
                                table.reload('roleTableId');
                            }else{
                                layer.msg(data.msg);
                            }
                        });

                    });
                }
            }
        });
        //监听表格复选框选择
        table.on('checkbox(roleTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(roleTableFilter)', function (obj) {
            var searchParam = JSON.stringify(form.val('data-search-form'));
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'edit.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    var ids = [];
                    ids.push(data.id);
                    var roleIds = JSON.stringify({roleIds:ids});
                    myPost("/sys-role/deleteByIds",roleIds,function(data){
                        if(data.code == 0){
                            layer.msg('删除成功', {icon: 1});
                            //执行搜索重载
                            table.reload('roleTableId', {
                                page: {
                                    curr: 1
                                }
                                , where: {
                                    searchParams: searchParam
                                }
                            }, 'data');
                        }else{
                            layer.msg('删除失败');
                        }
                    });
                    layer.close(index);
                });
            }else if(obj.event === 'role'){
                var index = layer.open({
                    title: '分配菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'role_menu.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

    });
</script>
</body>
</html>
